//8033b23c = timer for time stop
//8033b240 = ptr to function to run during time stop
//above are for complete pause of everything

//8033d480 = if it has byte 0x2 it starts time stop without mario being //frozen. [2=freeze everything but mario, or 0x200000 to 0x74 in obj struct]

//80384678

//80385c00

//8029cbc8

//8029cd28

//above functions are just chain of behavior parsing I used to look at this

//802D77DC
//PrintGenericText
//[A0 = int left, A1 = int bottom, A2 = pointer //sm64_string]


//jal 0x802d7384 //ortho matrix
//changes global to screen coords

//create translation matrix - 802d7070
//a0=matrix type [1 = push matrix, 2 = multiply matrix]
//a1=x translation as float
//a2=y translation as float
//a3=z translation as float

//create matrix - 802d7280
//a0=matrix type [1 = push matrix, 2 = multiply matrix]
//a1=x scale as float
//a2=y scale as float
//a3=z scale as float

//handles menu input with control stick - 802d862c
//a0=input type[1=vertical,2=horizontal]
//a1=*menuvalue
//a2=minvalue
//a3=maxvalue

//to create a text that scrolls one character at a 
//time just write the string one character at a time. 
//To give the appearance of a textbox just make your 
//own texrect at the correct location.
//add some controls inside the actual text data for 
//pauses and emphasis.

//text color:
//just use an env color right before it will color 
//the entire string. so basically, get end of master 
//DL - 8033b06c, store an env color there, update the 
//end of master DL ptr.

//b8 for master DL is written at 0x80247d14, only gives black box on jabo

//hook at 0x8027ba80 works for jabo for some reason (draw shadows function)

//hook at 8027806c is after all the movewords that start the master DL, none work

//80404ffC = lowercase

//8040505C = uppercase

/*/
keyboard sets:
lower:
00 9E 01 9E 02 9E 03 9E 04 9E 05 9E 06 9E 07 9E 08 9E 09 FE 34 9E 3A 9E 28 9E 35 9E 37 9E 3C 9E 38 9E 2C 9E 32 9E 33 FE 24 9E 36 9E 27 9E 29 9E 2A 9E 2B 9E 2D 9E 2E 9E 2F 9E E6 FE 3D 9E 3B 9E 26 9E 39 9E 25 9E 31 9E 30 9E E5 9E F4 9E 52 FE 50 9E 9E 9E 1C 19 0A 0C 0E 9E 9E 9E 0E 17 0D FF 



00 9E 01 9E 02 9E 03 9E 04 9E 05 9E 06 9E 07 9E 08 9E 09 FE 1A 9E 20 9E 0E 9E 1B 9E 1D 9E 22 9E 1E 9E 12 9E 18 9E 19 FE 0A 9E 1C 9E 0D 9E 0F 9E 10 9E 11 9E 13 9E 14 9E 15 9E E6 FE 23 9E 21 9E 0C 9E 1F 9E 0B 9E 17 9E 16 9E E5 9E F4 9E 52 FE 50 9E 9E 9E 1C 19 0A 0C 0E 9E 9E 9E 0E 17 0D FF
/*///



//40 to 4F is free
//70 to 9d is free
//use above numbers for special effects such as sounds and colors
//cutscenes/time stop will not enabled for this, that will have to be done seperately


//change 0xb9 value at 0x02011cc8 have CVG_X_ALPHA enabled so it works on pj64 video... why (not sure if will be needed in final)

//special text encoding cmds

//first is cmd byte, then parameter byte if necessary

//done
//40 set VIs per character, 2 byte - VIs per new char

//41 play sfx, 2 bytes - sfx arg

//42 set color of following characters, 4 bytes (white by default) - RGBA

//43 display short user string, 1 byte - user string number (each string is 10 bytes and goes from 6f00 to 6FF0, so inputs are 0 to F)

//44 display long user inputted string, up to FF characters

//45 return from user string

//46 make rainbow text (characters following cmd will rgb glow, call a new env color to disable) 1 byte, alpha value

//47 change text origin, 4 bytes - 2 bytes x pos, 2 bytes y pos

//48 jump string, 4 bytes - ram address (takes segmented and ram)

//49 translate text 4 bytes - 2 bytes x pos, 2 bytes y pos

//4C enable A press to increase text speed, 2 bytes - speed while holding A
//4D disable A press to jump to end of box

//4E re enable user input on re-talk (by default, a user input only works once, then the cmd acts as if it is not there, ofc this does not stay after resetting the game)

//4F allow user input of short string, 1 byte - short user string ID. (This cmd will remove all other input and bring a keyboard up the player can type on. If you are not in time stop mario will move around while typing and it will suck)

//boxes will reset scaling and position and color. The only thing preserved is the character speed, and the blip flags.
//70 end of box, auto go to next box (put a pause before this so your string stays a while)

//71 end of box, goto next box on a press

//72 remove all text for certain number of VIs, 2 bytes - num of VIs with no text (Text will flash back to its previous spot after it is done, so if you had three sentences, it will auto go back to that point once the cmd is done), 0 has no effect

//74 pause - 2 bytes VIs to wait

//76 play small blip sound per character (think like in megaman or any other rpg with text scrolling) call again to disable
//77 disable blip every character

//78 change music that plays after message to currently playing music (basically use this so if you switch tracks mid message, the track keeps playing, use after play music cmd)

//79 play music track, 1 byte - track number (original music will resume when string is done)

//7A cleanup buffer (Basically certain cmds like the keyboard, camera cmds, pause and remove on VI make use of a buffer so that the cmds don't repeat every frame. What happens is the cmds are replaced by padding, and their location are stored in the buffer which is at 807f6000 and can basically theoretically be 0xE00 bytes long. This cmd cleans the buffer, which re-enables those cmds. This means if the current box has a pause or keyboard and you use this cmd, it will play again. I advise using this before a jump string cmd or for weird mindtricks.)

//7B stop displaying string on A press (basically use this before an FF)

//7C stop displaying string after number of VI's (basically use this before an FF) - 2 bytes - num of VI's to wait until string is gone

//7F define background box, 2 bytes x start, 2 bytes y start, 2 bytes x end, 2 bytes y end. 4 bytes env color (coordinates go from the bottom right as the ori gin and increase)

//82 enable cutscene, 1 byte - cutscene index (zero to disable all cutscenes, others see my documentation at https://sites.google.com/view/supermario64romhacks/tweaks-and-tutorials?authuser=0)


//83 non buffer padding (basically for asm modifying the string)

//84 scale text, 4 bytes x scale float, 4 bytes y scale float (To compensate for large Y scaling, just do several line breaks the text will go farther up, e.g. y scale of 2 will need an empty line above it to not overlap, x will auto align)

//85 enable dialog options, 1 byte, number of options (they will be displayed vertically), the following strings will be the options, to use an user inputted string as an option do this (ex. 43 FF), returns byte of option selected (zero indexed)

//86 display following only on returned dialog option, 1 byte return, truncate string with FF, if return value does not match it will skip to following string, if there is no following string it will just display nothing (zero indexed)

//87 display general text (basically anything after this will display no matter what dialog option you choose, if you put another 86 cmd after this, general text will end and it will pass all text until another 87 or a matching 86)

//88 enable screen shake
//89 disable screen shake

//8A change camera location, 6 bytes - 2 bytes x, 2 bytes y, 2 bytes z (set once on the would be character it would draw, aka if a string goes like "mari[83 cmd]o" then the camera would be set when the "i" would be drawn to the screen)

//8B change camera focus, 6 bytes - 2 bytes x, 2 bytes y, 2 bytes z (set once on the would be character it would draw, aka if a string goes like "mari[83 cmd]o" then the camera would be set when the "i" would be drawn to the screen)

//8C set camera speed, 8 bytes - 2 bytes VIs, 2 bytes x, 2 bytes y, 2 bytes z (sets the change in camera values. Use to set a speed after defining a location. This means if you set y spd to 10, it will continue to go up until you change the location  all speeds are units/every second VI)

//8D set camera focus speed, 8 bytes - 2 bytes VIs, 2 bytes x, 2 bytes y, 2 bytes z (sets the change in camera values. Use to set a speed after defining a location. This means if you set y spd to 10, it will continue to go up until you change the location  all speeds are units/every second VI)

//8E lock camera on object, 4 bytes - *object


//a0 call function once, V0 is stored to array starting at 0x6d90, current string pos is stored to A1, 9 bytes -  4 bytes A1 arg, 4 bytes function Vaddresss, 1 byte return index

//a1 call function every frame while in box, V0 is stored to array starting at 0x6d90, current string pos is stored to A1, 9 bytes - 4 bytes A1 arg, 4 bytes function Vaddresss, 1 byte return index

//a2 display following only on matching function return, 1 byte - index of combined array (0 to 7 for A0 returns, 8 to 15 for A1 returns)


//not done


//too lazy right now to add

//80 define textured background box, 2 bytes width, 2 bytes height, 2 bytes alpha, 4 bytes texture ptr (coords are increasing, aka it will start 12 before the text start in both directions and increase by the param amount)

//81 disable all background boxes

//4A change origin over time, 6 bytes - 2 bytes length, 2 bytes x change, 2 bytes y change


//8F rotate camera around focus, 4 bytes - 2 bytes length, 2 bytes total rotation
//90 rotate camera in place, 4 bytes - 2 bytes length, 2 bytes total rotation

//91 rotate text
//92 rotate over time

//93 begin hide after time, 2 bytes length
//94 end hide flag
//95 begin show flag, 2 bytes length
//96 end show flag




//done
//9D is padding for buffer, (do not use without associated buffer function)
//FE is line break
//FF is end of string, this is used for dialog options and such. Use 7B and 7C to end full string



//80402860 will store jump table values for cmds if I ever get to it


//store temporary text starting from 0x807f7000
//6d00 VI of last character
//6d04 env color
//6d08 VI's per character
//6d0a will store state of keyboard input re input
//6d0c will store temp string ptr
//6d10 will store temp x
//6d12 will store temp y
//6d14 will store temp string end pos
//6d18 will store ptr to sfx
//6d1a will store check if blip is playing
//6d1c will store return ptr for user inputted string
//6d20 will store original music track
//6d22 will store switched to music
//6d24 will store VI timer for no display
//6d28 will store VI timer for string end
//6d2C will store VI timer for pause
//6d30 will store x scale
//6d34 will store y scale
//6d38 will store env color
//6d3C will store color for rainbow text
//6d40 will store string ptr for text removal
//6d44 will store flag for user input
//6d45 will store whether shift key is used
//6d46 will store which letter is being moved to
//6d47 will store which letter is selected
//6d48 will store VI timer for keyboard input
//6d4c will store ptr for keyboard removal
//6d50 will store location of user input
//6d54 will store number of lowercase letters
//6d58 will store returned dialog option
//6d59 will store currently displaying dialog option
//6d5A will store total number of options
//6d5B will store hovered dialog option
//6d5C will store ptr to end of all dialog options
//6d60 will store screen shake byte
//6d62 will store counter for VI display
//6d64 will store ptr to end of text in DL
//6d68 will store original pointer to string for buffer offset
//6d6C will store speed set with 40 if replaced (initialized to -1 if not set)
//6d70 will store camera location sweep start VI
//6d74 will store camera focus sweep start VI
//6d78 will store camera loc sweep flag
//6d7C will store camera foc sweep flag
//6d80 will store camera loc sweep end VI
//6d84 will store camera foc sweep end VI
//6d88 will store origin sweep start VI
//6d8C will store origin sweep end VI
//6d90 will store struct of returned single call function values (i*4+6d90), up to 8 values, zero indexed
//6db0 will store struct of returned every frame call function values (i*4+6db0), up to 8 values, zero indexed.

//not implemented yet
//6dd0 rotate camera thing or something
//6dd4 rotate camera thing or something
//6dd8 rotate text thing\

//implemented
//6ddc will store pointer to buffer position
//6df0 will be used as current position in the string
//6df4 will store x coord
//6df6 will store y coord
//6df8 will store the original string
//6dfc will store string end pos


//6000 will store cmd buffer (prob oversized)
//6e00 will store long string, up to FF characters (4E truncated)
//6f00 to 6ff0 - user inputted strings, 15 characters each (4E truncated)

//kerning for upper and lowercase letters is different, add variable that accounts for that, 5 is for lowercase, 7 for uppercase
//use VI to time string




//entry point
.orga 0x36A80
jal ADVANCEDTEXTENGFUNC //after shadows entry point that seems to work
nop


.headersize 0x7F200000

KEYBOARD_FILE equ "keyboard.bin"


//string controller function

.org 0x80403800
ADVANCEDTEXTENGFUNC:

addiu sp, sp, -0x20
sw ra, 0x14 (SP)

//check if there is a string, if not do nothing
lui s7, 0x807f
lw a2, 0x6df8 (S7) //ptr string
beq a2, r0, end
sw a2, 0x6d0c (S7)

lui at, 0x8034
lw v0, 0xb06c (AT) //current end of master DL
lw t0, 0x6d64 (S7) //end of last text
beq t0, v0, end
lh at, 0x6d62 (S7)
addiu t1, at, 1
beq at, r0, end
sh t1, 0x6d62 (S7)

//if there is a string, set it up
jal INSERTCHARSTARTDLFUNC //insert setup ptr
sw r0, 0x6d54 (S7)
lh a0, 0x6df4 (S7) //start x pos
lh a1, 0x6df6 (S7) //start y pos
sh a0, 0x6d10 (S7) //temp x pos
sh a1, 0x6d12 (S7) //temp y pos
lw at, 0x6dfc (S7) //string end pos
sw at, 0x6d14 (S7) //temp string end pos
sh r0, 0x6d18 (S7) //sfx ptr
lui at, 0x3f80
sw at, 0x6d30 (S7) //x scale
sw at, 0x6d34 (S7) //y scale
subiu at, r0, 0x1
sw at, 0x6d38 (S7) //env color
sw at, 0x6d6c (S7) //original VIs/char spd


lw s6, 0x8032D580 //current VI





lw at, 0x6d24 (S7)
bne at, r0, nodisplayonVI

lw t1, 0x6d44 (S7)
bne t1, r0, makeuserinput

//next parse string for special cmds and characters
parse:
lw a2, 0x6d0c (S7)
lw t1, 0x6df0 (S7) //current pos in string
addu t2, t1, a2
lbu t2, 0x0 (T2) //part in string to parse
sltiu t3, t2, 0x40 //1 if its less than 40
subiu t4, t2, 0x50
sltiu t4, t4, 0x20 //1 if its between 50 and 6f
subiu t5, t2, 0xd0
sltiu t5, t5, 0x2E //1 if its greater than D0
subiu t7, t2, 0x9e
sltiu t7, t7, 0x2
or t3, t3, t7 //catches space and dash
or t6, t4, t3 //will be 1 if its a normal character
or t5, t6, t5
bne t5, r0, normalchar
nop
//from here I will parse special cmds

lw t9, 0x6df0 (S7) //current pos in string 
lw t8, 0x6d0c (S7)
addu t8, t9, t8
lbu t7, 0x0 (T8) //char in string to read

li t3, 0xff
beq t3, t7, endofstring
li t2, 0xfe
beq t2, t7, linebreak

//should use a jump table for every other special

li t3, 0x40
beq t3, t7, setspeed
li t2, 0x42
beq t2, t7, setenvcolor
li t3, 0x70
beq t3, t7, AutoBoxEnd
li t2, 0x71
beq t2, t7, ABoxEnd
li t3, 0x41
beq t3, t7, setsfx
li t2, 0x44
beq t2, t7, longuserstring
li t3, 0x45
beq t3, t7, returnuserstring
li t2, 0x76
beq t2, t7, enableblip
li t3, 0x79
beq t3, t7, playmusictrack
li t2, 0x49
beq t2, t7, translatetext
li t3, 0x7B
beq t3, t7, removestringonA
li t2, 0x7C
beq t2, t7, removestringonVI
li t3, 0x74
beq t3, t7, pauseforVI
li t2, 0x77
beq t2, t7, disableblip
li t3, 0x84
beq t3, t7, scaletext
li t2, 0x7f
beq t2, t7, drawshadebox
li t3, 0x43
beq t3, t7, shortuserstring
li t2, 0x72
beq t2, t7, nodisplayonVI
li t3, 0x9D
beq t3, t7, blankout
li t2, 0x46
beq t2, t7, enablerainbowtext
li t3, 0x4f
beq t3, t7, userinputshortstring
li t2, 0x78
beq t2, t7, changeoriginalmusic
li t3, 0x85
beq t3, t7, startdialogoptions
li t2, 0x86
beq t2, t7, displayoncorrectdialogreturn
li t3, 0x88
beq t3, t7, shakescreen
li t2, 0x89
beq t2, t7, disablescreenshake
li t3, 0x4e
beq t3, t7, enablereinput
li t2, 0x87
beq t2, t7, blankout
li t3, 0x4c
beq t3, t7, speeduptext
li t2, 0x4d
beq t2, t7, disablefasttext
li t3, 0x47
beq t3, t7, translateorigin
li t2, 0x48
beq t2, t7, jumptext
li t3, 0x82
beq t3, t7, enablecutscene
li t2, 0x7a
beq t2, t7, clearbuffer
li t3, 0x8A
beq t3, t7, setcameralocation
li t2, 0x8B
beq t2, t7, setcamerafocus
li t3, 0x8C
beq t3, t7, setcamerasweep
li t2, 0x8D
beq t2, t7, setcamerafocussweep
li t3, 0x83
beq t3, t7, blankout
li t2, 0x8E
beq t2, t7, lockcameraonobj
li t3, 0x4A
beq t3, t7, blankout//sweeporigin
li t2, 0x4B
beq t2, t7, blankout//sweeptranslate
li t2, 0xa0
beq t2, t7, callasmonce
li t3, 0xa1
beq t3, t7, callasmeachframe
li t2, 0xa2
beq t2, t7, displayonmatchingreturn
nop


//42 cmd
setenvcolor:
jal SETENVCOLORFUNC //set env color
sw t8, 0x18 (SP)
lh a0, 0x6d10 (S7)
lh a1, 0x6d12 (S7)
jal 0x802d77dc //print string
ori a2, s7, 0x7000
jal FLUSHSTRINGFUNC //flushstring
lw t8, 0x18 (SP)
jal U32LOADFUNC //unaligned word load, t8 in, a0 out
nop
jal XPOSRESETFUNC //update x pos of string
sw a0, 0x6d38 (S7)
jal POPMATRIXFUNC //pop matrix
nop
jal SCALEMATRIXFUNC //push scale matrix
nop
jal FIXSTRINGTRANSLATIONFUNC //fix translation after scale
nop

lw t9, 0x6d0c (S7) //temp string
lw t1, 0x6df0 (S7) //current pos in string

lw t2, 0x6d14 (S7)
subu t2, t2, t1
sw t2, 0x6d14 (S7)
sw r0, 0x6df0 (S7)
sw r0, 0x6d54 (S7)
addu t9, t1, t9 //last byte read
addiu t9, t9, 0x5
beq r0, r0, parse
sw t9, 0x6d0c (S7)


//46 cmd
enablerainbowtext:
//print previous string
jal SETENVCOLORFUNC //set env color
sw t8, 0x18 (SP)
lh a0, 0x6d10 (S7)
lh a1, 0x6d12 (S7)
jal 0x802d77dc //print string
ori a2, s7, 0x7000
jal FLUSHSTRINGFUNC //flushstring
nop



//set rainbow color for next string
lw t3, 0x6d3C (S7)
bne t3, r0, oscillate
lw t4, 0x6d38 (S7)
sw t4, 0x6d3C (S7)


oscillate:
or s4, s7, r0
or v0, r0, s6
sin:
sw v0, 0x10 (SP)
mtc1 V0, f12 //VI timer
lui at, 0x3D20
cvt.s.w f12, f12
mtc1 at, f20
jal 0x80325480 //takes f12 returns f0
mul.s f12, f12, f20 //VI timer*modulation
lui t9, 0x42ff //127.5
mtc1 t9, f2
mul.s f4, f0, f2 //sin times 128
add.s f4, f2, f4 //sin oscillating 0-255
cvt.w.s f4, f4
mfc1 t1, f4
andi t2, s4, 0xff
sb t1, 0x6d3C (S4)
lw v0, 0x10 (SP)
addiu v0, v0, 0x78
addiu s4, s4, 0x1
li t3, 0x2
bne t3, t2, sin
lw t8, 0x18 (SP)
lbu t7, 0x1 (T8) //alpha value
sb t7, 0x6d3F (S7)
lw t6, 0x6d3C (S7)
sw t6, 0x6d38 (S7)


//set stuff up for next string
endoscillate:
jal XPOSRESETFUNC //update x pos of string
nop
jal POPMATRIXFUNC //pop matrix
nop
jal SCALEMATRIXFUNC //push scale matrix
nop
jal FIXSTRINGTRANSLATIONFUNC //fix translation after scale
nop

lw t9, 0x6d0c (S7) //temp string
lw t1, 0x6df0 (S7) //current pos in string

lw t2, 0x6d14 (S7)
subu t2, t2, t1
sw t2, 0x6d14 (S7)
sw r0, 0x6df0 (S7)
sw r0, 0x6d54 (S7)
addu t9, t1, t9 //last byte read
addiu t9, t9, 0x2
beq r0, r0, parse
sw t9, 0x6d0c (S7)



//47 cmd
translateorigin:
jal U16LOADFUNC //loads unaligned half, t8 in, t4 out
lw at, 0x6d0c (S7)
or t3, r0, t4
jal U16LOADFUNC
addiu t8, t8, 2
sh t3, 0x6df4 (S7)
sh t4, 0x6df6 (S7)
advancedbyfive:
addiu at, at, 0x5
beq r0, r0, parse
sw at, 0x6d0c (s7)


//48 cmd
jumptext:
jal U32LOADFUNC//unaligned word load
nop
jal 0x80277f50
nop
beq r0, r0, parse
sw v0, 0x6d0c (S7)


//49 cmd
translatetext:
jal SETENVCOLORFUNC //set env color
sw t8, 0x18 (SP) //
lh a0, 0x6d10 (S7)
lh a1, 0x6d12 (S7)
jal 0x802d77dc //print string
ori a2, s7, 0x7000
jal FLUSHSTRINGFUNC //flushstring
nop
jal XPOSRESETFUNC //update x pos of string
nop
jal POPMATRIXFUNC //pop matrix
nop
jal SCALEMATRIXFUNC //push scale matrix
nop
jal FIXSTRINGTRANSLATIONFUNC //fix translation after scale
nop
jal S16LOADFUNC //signed unaligned half load
nop
addiu t8, t8, 2
jal S16LOADFUNC
or t3, r0, a0 //SIGNED x pos

lh a2, 0x6d10 (S7)
lh a1, 0x6d12 (S7)
addu t3, a2, t3
addu t5, a1, a0
sh t3, 0x6d10 (S7)
sh t5, 0x6d12 (S7)
beq r0, r0, advancedbyfive
lw at, 0x6d0C (S7)


//4A cmd
sweeporigin:




//4C cmd
speeduptext:
lui t9, 0x8034
lhu t9, 0xafa0 (T9)
li t7, 0x8000
BNE t7, t9, nospeedup
lw at, 0x6d0c (S7)
lh t1, 0x6d08 (S7)
jal U16LOADFUNC //loads unaligned half, t8 in, t4 out
sh t1, 0x6d6C (S7)
sh t4, 0x6d08 (S7)
nospeedup:
addiu at, at, 3
beq r0, r0, parse
sw at, 0x6d0c (S7)


//4D cmd
disablefasttext:
lh t1, 0x6d6c (S7)
beq r0, r0, advancedbyone
sh t1, 0x6d08 (S7)


//84 cmd
scaletext:
jal SETENVCOLORFUNC //set env color
sw t8, 0x18 (SP)
lh a0, 0x6d10 (S7)
lh a1, 0x6d12 (S7)
jal 0x802d77dc //print string
ori a2, s7, 0x7000
jal FLUSHSTRINGFUNC //flushstring
nop

//by popping the matrix, all previous scalings are gone
jal POPMATRIXFUNC //pop matrix
nop

//update positions

jal XPOSRESETFUNC //update x pos of string
nop

//load new scales


jal U32LOADFUNC//unaligned word load
lw t8, 0x18 (SP)
sw a0, 0x6d30 (S7)
jal U32LOADFUNC//unaligned word load
addiu t8, t8, 4
sw a0, 0x6d34 (S7)

jal SCALEMATRIXFUNC //push scale matrix
nop
jal FIXSTRINGTRANSLATIONFUNC //fix translation after scale
nop
lw t9, 0x6d0c (S7) //temp string
lw t1, 0x6df0 (S7) //current pos in string
lw t2, 0x6d14 (S7)
subu t2, t2, t1
sw t2, 0x6d14 (S7) //temp string end pos
sw r0, 0x6df0 (S7)
sw r0, 0x6d54 (S7)
addu t9, t1, t9 //last byte read
addiu t9, t9, 0x9
beq r0, r0, parse
sw t9, 0x6d0c (S7)




//7F cmd
drawshadebox:
//make ortho matrix (don't need to call again after first one for text)
//pop mtx (to remove any previous scaling)
//env color
//scale push
//translate mult
//06 00 00 00 02 01 1c 48 (draws triangle
//pop matrix
//rescale+retranslate (so putting a box in the middle of a scaled string doesn't destroy it)


//draw previous string

jal SETENVCOLORFUNC //set env color
sw t8, 0x18 (SP)
lh a0, 0x6d10 (S7)
lh a1, 0x6d12 (S7)
jal 0x802d77dc //print string
ori a2, s7, 0x7000
jal FLUSHSTRINGFUNC //flushstring
nop

//pop mtx to remove any scaling on previous string
jal POPMATRIXFUNC //pop matrix
nop

//push scale matrix, push translation matrix

//W=xf-xi
//H=yf-yi
//Sx=W/7=(xf-xi)/7
//Sy=H/10=(yf-yi)/10

//Tx=xi/Sx=7*xi/(xf-xi)
//Ty=yi/Sy=10*yi/(yf-yi)



jal U16LOADFUNC //load unaligned half, t8 in, t4 out
lw t8, 0x18 (SP)
or t1, r0, t4 //xi
jal U16LOADFUNC //load unaligned half, t8 in, t4 out
addiu t8, t8, 0x2
or t2, r0, t4 //xf
jal U16LOADFUNC //load unaligned half, t8 in, t4 out
addiu t8, t8, 0x2
or t3, r0, t4 //yi
jal U16LOADFUNC //load unaligned half, t8 in, t4 out
addiu t8, t8, 0x2

//t4 is yf

subu t2, t2, t1 //W
mtc1 t2, f2
subu t4, t4, t3 //H
cvt.s.w f2, f2 //Width
mtc1 t4, f4
lui at, 0x40e0 //7
cvt.s.w f4, f4 //H
mtc1 at, f10 //7
lui at, 0x4120 //10
mtc1 at, f20 //10
div.s f22, f2, f10 //Sx
div.s f14, f4, f20 //Sy
mtc1 t1, f12 //xi
mtc1 t3, f16 //yi
cvt.s.w f12, f12
div.s f12, f12, f22 //Tx
cvt.s.w f16, f16
div.s f16, f16, f14 //Ty
swc1 f16, 0x1C (SP) //Ty
mfc1 a1, f22 //Sx
swc1 f12, 0x20 (SP) //Tx
mfc1 a2, f14 //Sy
lui a3, 0x3f80 //1 = scale
jal 0x802d7280 //scale matrix
ori a0, r0, 0x1 //push matrix
lw a1, 0x20 (SP)
lw a2, 0x1C (SP)
or a3, r0, r0
jal 0x802d7070 //translate matrix
ori a0, r0, 0x2 //push matrix

//store env color, make new set combine and draw the rectangle

lw t8, 0x18 (SP)
jal U32LOADFUNC//unaligned word load
addiu t8, t8, 0x8
lui at, 0x8034
lw v0, 0xb06c (AT)
li t1, 0xFC62EAC5 
sw t1, 0x0 (V0)
li t2, 0xFFD7FFFF
sw t2, 0x4 (V0)
lui t6, 0xFB00
sw t6, 0x8 (V0)
sw a0, 0xC (V0)
lui t9, 0x0600
sw t9, 0x10 (V0)
li t7, 0x02011d08 //char triangle
sw t7, 0x14 (V0)
lui t1, 0xbd00 //pop mtx
sw t1, 0x18 (V0)
sw r0, 0x1C (V0)
addiu v0, v0, 0x20
sw v0, 0xb06c (AT)


//setup stuff for the next string

jal INSERTCHARSTARTDLFUNC //init string
nop


jal XPOSRESETFUNC //update x pos of string
nop
jal POPMATRIXFUNC //pop matrix
nop
jal POPMATRIXFUNC //pop matrix
nop
jal SCALEMATRIXFUNC //push scale matrix
nop
jal FIXSTRINGTRANSLATIONFUNC //fix translation after scale
nop


lw t9, 0x6d0c (S7) //temp string
lw t1, 0x6df0 (S7) //current pos in string
lw t2, 0x6d14 (S7)
subu t2, t2, t1
sw t2, 0x6d14 (S7) //temp string end pos
sw r0, 0x6df0 (S7)
sw r0, 0x6d54 (S7)
addu t9, t1, t9 //last byte read
addiu t9, t9, 0xD
beq r0, r0, parse
sw t9, 0x6d0c (S7)


//82 cmd
enablecutscene:
lbu t3, 0x1 (T8)
lui t2, 0x8033
lw t1, 0xddcc (T2) //its always been 8033b90c as far as I can tell
lw t1, 0x24 (T1)
sb t3, 0x30 (T1) //cutscene controller

//a0 = location start
//a1 = num of bytes to add to buffer
or a0, r0, t8
jal ADDTOBUFFERFUNC
li A1, 2

lw t9, 0x6d0c (S7)
addiu t9, t9, 2
beq r0, r0, parse
sw t9, 0x6d0c (S7)



//not sure exactly how these work but I just store the intended positions in both the location and offset. If they aren't the same value then the camera skews.

//SET 1 8033c698 = object to focus on location

//SET 2 8033c6a4 = camera location

//SET 3 8033c6b0 = offset from focus object

//SET 4 8033c6bc = offset from cam location


//8A cmd
setcameralocation:
lui at, 0x8034
addiu at, at, 0xc
sw at, 0x20 (SP)
genericcameraset:

jal S16LOADFUNC //signed unaligned half load, t8 in, a0 out
nop
addiu t8, t8, 0x2
jal S16LOADFUNC //signed unaligned half load, t8 in, a0 out
or t1, r0, a0 //x pos
addiu t8, t8, 0x2
jal S16LOADFUNC //signed unaligned half load, t8 in, a0 out
or t2, r0, a0 //y pos

mtc1 t1, f0
mtc1 t2, f2
cvt.s.w f0, f0 //x pos
mtc1 a0, f4
cvt.s.w f2, f2 //y pos
cvt.s.w f4, f4 //z pos
lw at, 0x20 (SP)
swc1 f0, 0xc698 (at)
swc1 f0, 0xc6b0 (at)
swc1 f2, 0xc69c (at)
swc1 f2, 0xc6b4 (at)
swc1 f4, 0xc6a0 (at)
swc1 f4, 0xc6b8 (at)
lw t9, 0x6d0c (S7)
addiu t9, t9, 7
beq r0, r0, parse
sw t9, 0x6d0c (S7)


//8B cmd
setcamerafocus:
lui at, 0x8034
beq r0, r0, genericcameraset
sw at, 0x20 (SP)



//8C cmd
setcamerasweep:
jal U16LOADFUNC //load unaligned half, t8 in, t4 out
sw t8, 0x18 (SP)
//t4 is length in VIs
lw t9, 0x6d70 (S7)
bnel t9, r0, enablelocsweep
addiu t7, t8, 2

lw t6, 0x6d80 (S7)
beqz t6, skipnocheckfuck
subu t5, s6, t6
bgtzl t5, caplocsweep
addiu t8, t8, 2
skipnocheckfuck:
sw t8, 0x6d78 (S7) //flag for current sweep
addiu t7, t8, 2
sw s6, 0x6d70 (S7) //start VI
addu t3, t4, s6
sw t3, 0x6d80 (S7) //end VI

enablelocsweep:
lw t6, 0x6d78 (S7)
subu t5, t8, t6
bgtz t5, endcameralocsweep //if current addr is > start addr then exectute sweep
lw t9, 0x6d70 (S7)
bne t8, t6, caplocsweep
or t8, t7, r0
subu t6, s6, t9 //number of VIs that have passed since cmd started
subu t7, t4, t6 //VIs left in cmd
bgtz t7, nocapsweep
nop
sw r0, 0x6d70 (S7)
caplocsweep:
or t6, r0, t4
nocapsweep:
lui at, 0x8034
addiu at, at, 0xC
sw at, 0x20 (SP)


genericcamerasweep:
//t6 is num VIs sweep has lasted
srl t6, t6, 1 //div 2
mtc1 t6, f20
jal S16LOADFUNC
cvt.s.w f20, f20 //constant

or t1, r0, a0 //SIGNED x spd
mtc1 t1, f10
addiu t8, t8, 0x2
jal S16LOADFUNC
cvt.s.w f10, f10

or t2, r0, a0 //SIGNED y spd
mtc1 t2, f12
addiu t8, t8, 0x2
jal S16LOADFUNC
cvt.s.w f12, f12
mul.s f12, f12, f20

mtc1 a0, f14 //SIGNED z spd
lw at, 0x20 (SP)
cvt.s.w f14, f14

lwc1 f0, 0xc698 (at) //cam x
mul.s f14, f14, f20
lwc1 f2, 0xc69c (at) //cam y
mul.s f10, f10, f20
lwc1 f4, 0xc6a0 (at) //cam z

add.s f0, f0, f10
add.s f2, f2, f12
add.s f4, f4, f14


storecameravars:
swc1 f0, 0xc698 (at)
swc1 f0, 0xc6b0 (at)
swc1 f2, 0xc69c (at)
swc1 f2, 0xc6b4 (at)
swc1 f4, 0xc6a0 (at)
swc1 f4, 0xc6b8 (at)

endcameralocsweep:
lw at, 0x6d0c (S7)
addiu at, at, 0x9
beq r0, r0, parse
sw at, 0x6d0c (S7)




//8D cmd
setcamerafocussweep:
jal U16LOADFUNC //load unaligned half, t8 in, t4 out
sw t8, 0x18 (SP)
//t4 is length in VIs
lw t9, 0x6d74 (S7)
bnel t9, r0, enablefocsweep
addiu t7, t8, 2

lw t6, 0x6d84 (S7)
beqz t6, skipcheckfuckshitcode
subu t5, s6, t6
bgtzl t5, capfocsweep
addiu t8, t8, 2
skipcheckfuckshitcode:
sw t8, 0x6d7C (S7) //flag for current sweep
addiu t7, t8, 2
sw s6, 0x6d74 (S7) //start VI
addu t3, t4, s6
sw t3, 0x6d84 (S7) //end VI

enablefocsweep:
lw t6, 0x6d7C (S7)
subu t5, t8, t6
bgtz t5, endcameralocsweep //if current addr is > start addr then exectute sweep
lw t9, 0x6d74 (S7)
bne t8, t6, capfocsweep
or t8, t7, r0
subu t6, s6, t9 //number of VIs that have passed since cmd started
subu t7, t4, t6 //VIs left in cmd
bgtz t7, nocapFsweep
nop
sw r0, 0x6d74 (S7)
capfocsweep:
or t6, r0, t4
nocapFsweep:
lui at, 0x8034
beq r0, r0, genericcamerasweep
sw at, 0x20 (SP)


//8E cmd
lockcameraonobj:
jal U32LOADFUNC//unaligned word load
lw t5, 0x6d0c (S7)
subiu t5, t5, 4
sw t5, 0x6d0c (S7)
lwc1 f0, 0xa0 (A0)
lwc1 f2, 0xa4 (A0)
lwc1 f4, 0xa8 (A0)
beq r0, r0, storecameravars
lui at, 0x8034



//7A cmd
clearbuffer:
jal CLEANUPBUFFERFUNC //cleanup buffer
lui s7, 0x807f
beq r0, r0, advancedbyone
nop



//4F cmd
userinputshortstring:

//set combine for selected text is FC12EA25FFD7FFFF
//2A is the max value for the selector


lw at, 0x6d44 (S7) //flag for user input
BNE at, r0, makeuserinput
li t6, 0x1
sw r0, 0x6d50 (S7)
sh t6, 0x6d46 (S7)
sw t8, 0x6d4C (S7)
lbu t7, 0x1 (T8)
li t9, 0x10
multu t9, t7
mflo t9
addu t9, t9, s7
addiu t9, t9, 0x6f00
li t4, 0x9f9f9f9f
sw t4, 0x0 (T9)
li t3, 0x9f9f9fFF
sw t4, 0x4 (T9)
sw t4, 0x8 (T9)
sw t3, 0xC (T9)
beq r0, r0, printnone
sb T7, 0x6d44 (S7)



makeuserinput:

//draw background boxes for user input and keyboard
lui a1, 0x41F8
lui a2, 0x40E0
lui a3, 0x3f80 //1 = scale
jal 0x802d7280 //scale matrix
ori a0, r0, 0x1 //push matrix

lui a1, 0x3f80
or a2, r0, r0
or a3, r0, r0
jal 0x802d7070 //translate matrix
ori a0, r0, 0x2 //push matrix

lui at, 0x8034
lw v0, 0xb06c (AT)
li t1, 0xFC62EAC5 
sw t1, 0x0 (V0)
li t2, 0xFFD7FFFF
sw t2, 0x4 (V0)
lui t6, 0xFB00
sw t6, 0x8 (V0)
li t9, 0x60606060
sw t9, 0xC (V0)
lui t9, 0x0600
sw t9, 0x10 (V0)
li t7, 0x02011d08 //char triangle
sw t7, 0x14 (V0)
addiu v0, v0, 0x18
sw v0, 0xb06c (AT)


lui a1, 0x3f80
lui a2, 0x3EC0
lui a3, 0x3f80 //1 = scale
jal 0x802d7280 //scale matrix
ori a0, r0, 0x2 //mult matrix


or a1, r0, r0
lui a2, 0x4250
or a3, r0, r0
jal 0x802d7070 //translate matrix
ori a0, r0, 0x2 //push matrix

lui at, 0x8034
lw v0, 0xb06c (AT)
lui t9, 0x0600
sw t9, 0x0 (V0)
li t7, 0x02011d08 //char triangle
sw t7, 0x4 (V0)
addiu v0, v0, 0x8
sw v0, 0xb06c (AT)

jal POPMATRIXFUNC //pop matrix
nop

jal INSERTCHARSTARTDLFUNC //init string
nop

jal FLUSHSTRINGFUNC //flushstring
nop

lw at, 0x6d48 (S7)
beq at, s6, makekeyboarddisplay

li t0, 6
andi t2, s6, 0x6
bne t2, t0, beginkeyboard


//control selected text with control stick
li a0, 0x2 //horizontal
ori a1, s7, 0x6d47 //*menuvalue
li a2, 0x0 //min
jal 0x802d862c
li a3, 0x2b//max value

li a0, 0x1 //vert
ori a1, s7, 0x6d46 //*menuvalue
li a2, -1 //min
jal 0x802d862c
li a3, 0x1//max value

lb t0, 0x6d46 (S7)
lbu t1, 0x6d47 (S7)
li t2, 0xa
multu t2, t0
mflo t0
addu t0, t0, t1
sltiu t3, t0, 0x2c
bne t3, r0, capcontrolstickvert
nop
li t0, 0x2b
capcontrolstickvert:
sh t0, 0x6d46 (S7)


//control currently selected text with d pad

lh at, 0x6d46 (S7) //currently selected text
lui t0, 0x8034
lh t0, 0xafa0 (T0) //new btns
li t9, 0x0200 // D Left
bne t9, t0, checkDright
subiu t1, at, 0x1
beq r0, r0, beginkeyboard
sh t1, 0x6d46 (S7)
checkDright:
li t9, 0x0100
bne t9, t0, checkDup
addiu t1, at, 0x1
beq r0, r0, beginkeyboard
sh t1, 0x6d46 (S7)

checkDup:
li t9, 0x800 //Dup
bne t9, t0, checkDdown
subiu t1, at, 0x0a
beq r0, r0, beginkeyboard
sh t1, 0x6d46 (S7)
checkDdown:
li t9, 0x0400 //Ddown
bne t9, t0, beginkeyboard
sltiu t0, at, 0x21
beq t0, r0, ddowntospace
addiu t1, at, 0x0a
beq r0, r0, beginkeyboard
sh t1, 0x6d46 (S7)

ddowntospace:
li t1, 0x2a
sh t1, 0x6d46 (S7)

beginkeyboard:
//check for overflow or underflow on selected
lh t9, 0x6d46 (S7) //currently selected text
subiu t0, t9, 0x1
bltz t0, keyboardunderflow
sltiu at, t9, 0x2C
beq at, r0, keyboardoverflow
nop
beq r0, r0, makekeyboarddisplay
keyboardoverflow:
li t8, 0x1
beq r0, r0, makekeyboarddisplay
sh t8, 0x6d46 (S7)
keyboardunderflow:
li t7, 0x2B
sh t7, 0x6d46 (S7)



//80404ffC = lowercase
//8040505C = uppercase

makekeyboarddisplay:

lui a1, 0x4000
lui a2, 0x3f9A
lui a3, 0x3f80 //1 = scale
jal 0x802d7280 //scale matrix
ori a0, r0, 0x1 //push matrix

lh t9, 0x6d46 (S7) //currently selected text
lb t7, 0x6d45 (S7) //shift selected or not
lui at, 0x8040
beq t7, r0, lowercase
ori s5, at, KEYBOARDSTARTLOC-0X80400000//LOWERCASE
ori s5, at, KEYBOARDSTARTLOC-0X80400000+filesize("keyboard.bin")/2 //uppercaseset
lowercase:
or t5, r0, r0
or t8, r0, s7
or s2, r0, r0//letter spacing counter
or s3, r0, r0 //line break counter
loopkeyboard1:
li t2, 0x2B
beql t2, t9, noaccountforspace
addiu t9, t9, 0x4
noaccountforspace:
lbu t1, 0x0 (S5)
li t2, 0x9e //space
beq t2, t1, nocountforselector1
li t3, 0xfe
bne t3, t1, countforselector1
nop
subiu s2, r0, 0x1
beq r0, r0, nocountforselector1
subiu s3, s3, 0x10

countforselector1:
addiu t5, t5, 0x1

nocountforselector1:
li t3, 0xff
sb t3, 0x7001 (T8)
beq t3, t1, breakkeyboardloop1
addiu t8, t8, 0x1
beq t5, t9, breakkeyboardloop1
nop
addiu s2, s2, 0x1
addiu s5, s5, 0x1
beq r0, r0, loopkeyboard1
sb t1, 0x7000 (t8)
breakkeyboardloop1:



li a0, 0x12
li a1, 0x46
lui a2, 0x807f
jal 0x802d77dc //print string
ori a2, A2, 0x7000

jal FLUSHSTRINGFUNC //flushstring
nop

//change setcombine so that black background goes around current letter
//2A = space
//2B = end


li t1, 0xFC12EA25
li t0, 0xFFD7FFFF
lui at, 0x8034
lw v0, 0xb06c (AT)
sw t1, 0x0 (V0)
sw t0, 0x4 (V0)
addiu v0, v0, 0x8
sw v0, 0xb06c (AT)

//print single letter with black background

li t6, 0
lui t8, 0x807f
lh t9, 0x6d46 (S7)
li at, 0x2b
beq at, t9, endletter
li t0, 0x2a
bne t0, t9, singleletter
nop
addiu s2, s2, 0x1
space:
li t3, 0xff
lbu t1, 0x0 (S5)
addiu S5, S5, 0x1
addiu t6, t6, 0x1
sb t3, 0x7001 (T8)
li t0, 0x5
sb t1, 0x7000 (T8)
bne t0, t6, space
addiu t8, t8, 0x1
beq r0, r0, printhighlightedletter
nop

endletter:
li t3, 0xff
lbu t1, 0x0 (S5)
addiu S5, S5, 0x1
addiu t6, t6, 0x1
sb t3, 0x7001 (T8)
li t0, 0x3
sb t1, 0x7000 (T8)
bne t0, t6, endletter
addiu t8, t8, 0x1
beq r0, r0, printhighlightedletter



singleletter:
li t3, 0xff
lbu t1, 0x0 (S5)
addiu S5, S5, 0x1
sb t3, 0x7001 (S7)
sb t1, 0x7000 (S7)



printhighlightedletter:
mtc1 s2, f2
cvt.s.w f2, f2
lh t9, 0x6d46 (S7)
sltiu t9, t9, 0xb
bne t9, r0, smallerkerning1
lui at, 0x40C8
lb t0, 0x6d45 (S7)
beq t0, r0, smallerkerning1
lui at, 0x40B0
lui at, 0x40C8
smallerkerning1:
mtc1 at, f4
mul.s f2, f2, f4
cvt.w.s f2, f2
mfc1 t0, f2


li a0, 0x12
addu a0, a0, t0
li a1, 0x46
addu a1, a1, s3
lui a2, 0x807f
jal 0x802d77dc //print string
ori a2, A2, 0x7000

jal FLUSHSTRINGFUNC //flushstring
nop

jal INSERTCHARSTARTDLFUNC //init string
addiu s2, s2, 0x1


lui a1, 0x4000
lui a2, 0x3f9A
lui a3, 0x3f80 //1 = scale
jal 0x802d7280 //scale matrix
ori a0, r0, 0x1 //push matrix



lui t8, 0x807f
loopkeyboard2:
lbu t1, 0x0 (S5)
li t3, 0xfe
beq t3, t1, breakkeyboardloop2
li t2, 0xff
sb t1, 0x7000 (t8)
beq t2, t1, breakkeyboardloop3
sb t2, 0x7001 (T8)
addiu t8, t8, 0x1
bne t5, t9, loopkeyboard2
addiu s5, s5, 0x1
breakkeyboardloop2:



mtc1 s2, f2
cvt.s.w f2, f2
lh t9, 0x6d46 (S7)
sltiu t9, t9, 0xb
bne t9, r0, smallerkerning2
lui at, 0x40C8
lb t0, 0x6d45 (S7)
beq t0, r0, smallerkerning2
lui at, 0x40B0
lui at, 0x40C8
smallerkerning2:
mtc1 at, f4
mul.s f2, f2, f4
cvt.w.s f2, f2
mfc1 t0, f2



li a0, 0x12
addu a0, a0, t0
li a1, 0x46
addu a1, a1, s3
lui a2, 0x807f
jal 0x802d77dc //print string
ori a2, A2, 0x7000



lui t8, 0x807f
loopkeyboard3:
lbu t1, 0x0 (S5)
sb t1, 0x7000 (t8)
li t3, 0xff
sb t3, 0x7001 (T8)
beq t3, t1, breakkeyboardloop3
addiu t8, t8, 0x1
bne t5, t9, loopkeyboard3
addiu s5, s5, 0x1
breakkeyboardloop3:


lh t9, 0x6d46 (S7)
li t7, 0x29
bne t7, t9, checkspacebarspace
li a0, 0x12
beq r0, r0, printfinalkeyboard
addiu a0, a0, 0x18
checkspacebarspace:
li t8, 0x2a
bne t8, t9, printfinalkeyboard
li a0, 0x12
addiu a0, a0, 0x46

printfinalkeyboard:
li a1, 0x46
addu a1, a1, s3
lui a2, 0x807f
jal 0x802d77dc //print string
ori a2, A2, 0x7000



//keyboard has been properly displayed, now I will print the string I am writing above

lb T7, 0x6d44 (S7)
li t9, 0x10
multu t7, t9
mflo t9
addu t9, t9, s7
addiu t9, t9, 0x6f00 //address of string


//display user string

li a0, 0x18
li a1, 0x76
jal 0x802d77dc //print string
or a2, r0, T9

jal POPMATRIXFUNC //pop matrix
nop

//add to string on A press
lw at, 0x6d48 (S7)
beq at, s6, printnone
nop
sw s6, 0x6d48 (S7)
lbu t7, 0x6d44 (S7)
li t6, 0x10
multu t6, t7
mflo t6
addu t6, t6, s7
addiu t6, t6, 0x6f00
lw t8, 0x6d50 (S7) //spot in user string
addu t6, t8, t6
lh t9, 0x6d46 (S7)
lui t0, 0x8034
lh t0, 0xafa2 (T0)
andi t2, t0, 0x4000
bne t2, r0, backspace
andi t1, t0, 0x8000
beq t1, r0, printnone
li at, 0x2b
beq at, t9, endkeyboard
li t1, 0x29
beq t1, t9, shift
li at, 0x2a
beq at, t9, addspace
li t1, 0x28
beq t1, t9, backspace
li at, 0xF
beq at, t8, printnone



//80404ffC = lowercase
//8040505C = uppercase

//every other letter
lb t7, 0x6d45 (S7) //shift selected or not
ori t0, r0, 0x1
lui at, 0x8040
beq t7, r0, lowercaseinput
ori s5, at, 0x4ffC //lowercaseset
ori s5, at, 0x505C //uppercaseset


lowercaseinput:
lbu t4, 0x0 (S5)
addiu s5, s5, 0x1
li t3, 0x9e
beq t3, t4, lowercaseinput
li t2, 0xfe
beq t2, t4, lowercaseinput
nop
beq t0, t9, addletterinput
nop
beq r0, r0, lowercaseinput
addiu t0, t0, 0x1


addletterinput:
sb t4, 0x0 (T6)
addiu t8, t8, 0x1
beq r0, r0, printnone
sw t8, 0x6d50 (S7)


backspace:
blez t8, printnone
subiu t6, t6, 0x1
li t2, 0x9f
sb t2, 0x0 (T6)
subiu t8, t8, 0x1
beq r0, r0, printnone
sw t8, 0x6d50 (S7)



shift:
lb t2, 0x6d45 (S7)
xori t2, t2, 0x1
beq r0, r0, printnone
sb t2, 0x6d45 (S7)


addspace:
li at, 0xF
beq at, t8, printnone
li t2, 0x9e
sb t2, 0x0 (T6)
addiu t8, t8, 0x1
beq r0, r0, printnone
sw t8, 0x6d50 (S7)



//end keyboard, save input

endkeyboard:
li t2, 0x45
sb t2, 0x0 (T6)
lw t8, 0x6d4C (S7)
sw r0, 0x6d50 (S7)
sw r0, 0x6d4C (S7)
sw r0, 0x6d44 (S7)
sw r0, 0x6d48 (S7)
lw at, 0x6d0c (S7) //temp string
addiu at, at, 0x2
sw at, 0x6d0c (S7) //temp string

lh at, 0x6d0a (S7)
beq at, r0, paddoutkeyboard

//a0 = location start
//a1 = num of bytes to add to buffer
or a0, r0, t8
jal ADDTOBUFFERFUNC
li A1, 2

beq r0, r0, parse

paddoutkeyboard:
li t9, 0x9d
sb t9, 0x0 (T8)
sb t9, 0x1 (T8)
beq r0, r0, parse
sh r0, 0x6d0a (S7)








//9D cmd
blankout:
beq r0, r0, advancedbyone




//72 cmd
nodisplayonVI:
lw at, 0x6d24 (S7) //VI timer for no display
BNE at, r0, checkVItimefordisplay
nop
sw s6, 0x6d24 (S7)
beq r0, r0, printnone
sw t8, 0x6d40 (S7)
checkVItimefordisplay:
jal U16LOADFUNC //load unaligned half, t8 in, t4 out
lw t8, 0x6d40 (S7)
addu at, t4, at //VI no display should end at
subu at, s6, at
bltz at, printnone
lw t1, 0x6d0c (S7) //temp string
addiu t4, t1, 0x3
sw r0, 0x6d24 (S7)

//a0 = location start
//a1 = num of bytes to add to buffer
or a0, r0, t8
jal ADDTOBUFFERFUNC
li A1, 3

beq r0, r0, parse
sw t4, 0x6d0c (S7) //temp string


//4E cmd
enablereinput:
li t9, 1
beq r0, r0, advancedbyone
sh t9, 0x6d0a (S7)



//74 cmd
pauseforVI:
lw at, 0x6d2C (S7)
BEQL at, r0, printnone
sw s6, 0x6d2C (S7)
jal U16LOADFUNC //load unaligned half, t8 in, t4 out
nop
addu at, t4, at //VI that the pause should end at
subu at, s6, at
bltz at, nonewchar
lw t1, 0x6d0c (S7) //temp string
addiu t4, t1, 0x3
sw r0, 0x6d2c (S7)

//a0 = location start
//a1 = num of bytes to add to buffer
or a0, r0, t8
jal ADDTOBUFFERFUNC
li A1, 3

beq r0, r0, parse
sw t4, 0x6d0c (S7) //temp string



//76 cmd
enableblip:
lh t9, 0x6d1A (S7)
ori t9, t9, 0x1
beq r0, r0, advancedbyone
sh t9, 0x6d1A (S7)



//77 cmd
disableblip:
beq r0, r0, advancedbyone
sh r0, 0x6d1A (S7)



//78 cmd
changeoriginalmusic:
lh t2, 0x6d22 (S7) //switched to music
beq r0, r0, advancedbyone
sh t2, 0x6d20 (S7) //original music


//79 cmd
playmusictrack:
lui at, 0x8034
lh t1, 0xb944 (AT) //currently playing music
lh t2, 0x6d20 (S7) //original music
bltzl t2, skipsetogmusic
sh t1, 0x6d20 (S7)
skipsetogmusic:
lbu a1, 0x1 (T8) //music we want to play
lh t3, 0x6d22 (S7) //switched to music
beq t3, a1, skipenablemusic
or a0, r0, r0
sh a1, 0xb944 (AT) //currently playing
jal 0x80320544 //set music
sh a1, 0x6d22 (S7) //switched to music
skipenablemusic:
lw at, 0x6d0c (S7) //temp string
addiu at, at, 0x2
beq r0, r0, parse
sw at, 0x6d0c (S7) //temp string



//7B cmd
removestringonVI:
lw at, 0x6d28 (S7)
BEQL at, r0, nocheckVIforstringend
sw s6, 0x6d28 (S7)
jal U16LOADFUNC //load unaligned half, t8 in, t4 out
nop
addu at, t4, at //VI string should end at
subu at, s6, at
blez at, nocheckVIforstringend
nop
jal COMPLETESTRINGRESETFUNC //resetstring
sw r0, 0x6d2c (S7)
beq r0, r0, end
sw r0, 0x6d24 (S7)
nocheckVIforstringend:
li t7, 0xff
lw t9, 0x6df0 (S7) //current pos in string
addu t9, t9, s7
beq r0, r0, nonewchar
sb t7, 0x7000 (T9)



//7C cmd
removestringonA:
lui t9, 0x8034
lh t9, 0xafa2 (T9) //new buttons
andi t0, t9, 0x8000 //a press
//flash down arrow
andi t1, s6, 0x20
beq r0, t1, noarrowforend7c
li t7, 0xff
li t7, 0x51
noarrowforend7c:
li t6, 0xff
lw t9, 0x6df0 (S7) //current pos in string
addu t9, t9, s7
sb t6, 0x7001 (T9)
beq t0, r0, nonewchar
sb t7, 0x7000 (T9)
jal COMPLETESTRINGRESETFUNC //resetstring
nop
beq r0, r0, end
nop


//85 cmd
startdialogoptions:

//print string before dialog options

jal SETENVCOLORFUNC //set env color
sw t8, 0x18 (SP)
lh a0, 0x6d10 (S7)
lh a1, 0x6d12 (S7)
jal 0x802d77dc //print string
ori a2, s7, 0x7000
jal FLUSHSTRINGFUNC //flushstring
nop
lh a0, 0x6df4 (S7)
jal POPMATRIXFUNC //pop matrix
sh a0, 0x6d10 (S7)
jal SCALEMATRIXFUNC //push scale matrix
nop
jal FIXSTRINGTRANSLATIONFUNC //fix translation after scale
nop

//6d58 will store returned dialog option
//6d59 will store currently displaying dialog option
//6d5A will store total number of options
//6d5B will store hovered dialog option


//line break for first option to be under question dialog
lh a1, 0x6d12 (S7) //temp y pos
subiu a1, a1, 0xb
sh a1, 0x6d12 (S7)

//store total num of options
lw t8, 0x18 (SP)
lbu t0, 0x1 (T8) //number of options
sb t0, 0x6d5a (S7)

//check for dialog selection
lw t0, 0x6d5C (S7)
beq t0, r0, setmenuvaluesdialog
lui t9, 0x8034
lh t9, 0xafa2 (T9)
andi t9, t9, 0x8000
beq t9, r0, setmenuvaluesdialog
lbu t0, 0x6d5b (S7)
sb t0, 0x6d58 (S7)
sh r0, 0x6d5A (S7)
sb r0, 0x6d59 (S7)
lw t9, 0x6d5C (S7)
sw r0, 0x6d5c (S7)
sw r0, 0x6df0 (S7)
sw r0, 0x6d54 (S7)
sw r0, 0x6dfc (S7)
beq r0, r0, printnone
sw t9, 0x6df8 (S7) //*string



setmenuvaluesdialog:
//update end of string ptr
sb r0, 0x6d59 (S7)
lw t1, 0x6df0 (S7)
lw t0, 0x6d14 (S7)
subu t0, t0, t1
sw t0, 0x6d14 (S7)




//get control stick input for menu selection
li a0, 0x1 //vert
ori a1, s7, 0x6d5B //*menuvalue
li a2, 0 //min
jal 0x802d862c
lbu a3, 0x6d5a (S7) //max value

//draw arrow if option 0 is selected

lb t0, 0x6d5b (S7)
bne t0, r0, nosetarrowdialog
li t1, 0x53
li at, 0xff
sb at, 0x7001 (S7)
jal SETENVCOLORFUNC //set env color
sb t1, 0x7000 (S7)
lh a0, 0x6df4 (S7)
lh a1, 0x6d12 (S7) //temp y pos
jal 0x802d77dc //print string
ori a2, s7, 0x7000
jal FLUSHSTRINGFUNC //flushstring
nop
jal POPMATRIXFUNC //pop matrix
nop
jal SCALEMATRIXFUNC //push scale matrix
nop
jal FIXSTRINGTRANSLATIONFUNC //fix translation after scale
nop

nosetarrowdialog:

//set x pos to be init pos for line break
lh a0, 0x6df4 (S7)
sh a0, 0x6d10 (S7)
li t0, 0x1
sw r0, 0x6d54 (S7)
jal XPOSRESETFUNC //update x pos of string
sw t0, 0x6dF0 (S7)
sw r0, 0x6dF0 (S7)
lw at, 0x18 (SP) //*temp string
addiu at, at, 0x2
beq r0, r0, parse
sw at, 0x6d0c (S7) //*temp string 



//86 cmd
displayoncorrectdialogreturn:
lbu t1, 0x1 (T8)
lbu t0, 0x6d58 (S7)
lw t2, 0x6d0c (S7)
addiu t2, t2, 0x2
beq t1, t0, parse
sw t2, 0x6d0c (S7)

notcorrectoption:
addiu t8, t8, 0x1
lbu t0, 0x0 (T8)
li t2, 0x87
beql t0, t2, generaltextdisplay
addiu t7, t8, 1
li t3, 0x86
bne t0, t3, notcorrectoption
or t7, t8, r0
generaltextdisplay:
jal SETENVCOLORFUNC //set env color
sw t7, 0x18 (SP)
lh a0, 0x6d10 (S7)
lh a1, 0x6d12 (S7)
jal 0X802d77dc //print string
ori a2, s7, 0x7000
jal FLUSHSTRINGFUNC //flushstring
lw v1, 0x6df0 (S7) //current pos in string
jal POPMATRIXFUNC //pop matrix
lw gp, 0x6d14 (S7)
jal SCALEMATRIXFUNC //push scale matrix
subu gp, gp, v1
jal FIXSTRINGTRANSLATIONFUNC //fix translation after scale
sw gp, 0x6d14 (S7)
jal XPOSRESETFUNC //update x pos of string
lw gp, 0x18 (SP)
sw r0, 0x6df0 (S7)
beq r0, r0, parse
sw gp, 0x6d0c (S7)


//a0 cmd
callasmonce:
jal U32LOADFUNC //get a0 arg
sw t8, 0x18 (SP)
or a2, r0, a0
or a1, r0, t8 //ptr string pos
jal U32LOADFUNC //get asm add
addiu t8, t8, 4
or t9, r0, a0 //function address
jalr t9
or a0, r0, a2 //user defined arg
lw t8, 0x18 (SP)
lbu t7, 0x9 (T8)
sll t7, t7, 2
addu at, s7, t7
sw v0, 0x6d90 (AT)
or a0, r0, t8
jal ADDTOBUFFERFUNC
li a1, 10
lw t0, 0x6d0c (S7)
addiu t0, t0, 10
b parse
sw t0, 0x6d0c (S7)



//a1 cmd
callasmeachframe:
jal U32LOADFUNC //get a0 arg
sw t8, 0x18 (SP)
or a2, r0, a0
or a1, r0, t8 //ptr string pos
jal U32LOADFUNC //get asm add
addiu t8, t8, 4
or t9, r0, a0 //function address
jalr t9
or a0, r0, a2 //user defined arg
lw t8, 0x18 (SP)
lbu t7, 0x9 (T8)
sll t7, t7, 2
addu at, s7, t7
sw v0, 0x6dB0 (AT)
lw t0, 0x6d0c (S7)
addiu t0, t0, 10
b parse
sw t0, 0x6d0c (S7)


//a2 cmd
displayonmatchingreturn:
jal U32LOADFUNC
addiu t8, t8, 1
lbu t1, 0x0 (T8)
sll t1, t1, 2
addu at, t1, s7
lw t0, 0x6d90 (S7) //returned value
lw t2, 0x6d0c (S7)
addiu t8, t8, 4
sw t8, 0x18 (SP)
addiu t2, t2, 0x6
beq A0, t0, parse
sw t2, 0x6d0c (S7)

notcorrectreturn:
addiu t8, t8, 0x1
lbu t0, 0x0 (T8)
li t2, 0x87
beql t0, t2, generaltextdisplay
addiu t7, t8, 1
li t3, 0xA2
bne t0, t3, notcorrectreturn
or t7, t8, r0
lw t6, 0x18 (SP)
subu t5, t7, t6
lw t1, 0x6d0c (S7)
addu t1, t1, t5
subiu t1, t1, 1
b displayonmatchingreturn
sw t1, 0x6d0c (S7)



//88 cmd
shakescreen:
li at, 1
sb at, 0x6d60 (s7)
advancedbyone:
lw t0, 0x6d0c (S7)
addiu t0, t0, 0x1
beq r0, r0, parse
sw t0, 0x6d0c (S7)

//89 cmd
disablescreenshake:
beq r0, r0, advancedbyone
sb r0, 0x6d60 (s7)


//44 cmd
longuserstring:
lw at, 0x6d0c (S7) //temp string
addiu at, at, 0x1
sw at, 0x6d1c (S7) //*string return loc
ori t1, s7, 0x6e00
beq r0, r0, parse
sw t1, 0x6d0c (S7)


//43 cmd
shortuserstring:
jal SETENVCOLORFUNC //set env color
sw t8, 0x18 (SP)
lh a0, 0x6d10 (S7)
lh a1, 0x6d12 (S7)
jal 0x802d77dc //print string
ori a2, s7, 0x7000
jal FLUSHSTRINGFUNC //flushstring
nop
jal XPOSRESETFUNC //update x pos of string
nop
jal POPMATRIXFUNC //pop matrix
nop
jal SCALEMATRIXFUNC //push scale matrix
nop
jal FIXSTRINGTRANSLATIONFUNC //fix translation after scale
nop
lw t1, 0x6df0 (S7) //current pos in string
lw t2, 0x6d14 (S7)
subu t2, t2, t1
sw t2, 0x6d14 (S7)
sw r0, 0x6df0 (S7)
sw r0, 0x6d54 (S7)
lw t8, 0x18 (SP)
lw at, 0x6d0c (S7) //temp string
addu at, at, t1
addiu at, at, 0x2
sw at, 0x6d1c (S7) //*string return loc
ori t1, s7, 0x6f00
lbu t9, 0x1 (T8)
li at, 0x10
multu t9, at
mflo at
addu t1, t1, at
beq r0, r0, parse
sw t1, 0x6d0c (S7)


//45 cmd
returnuserstring:
jal SETENVCOLORFUNC //set env color
sw t8, 0x18 (SP)
lh a0, 0x6d10 (S7)
lh a1, 0x6d12 (S7)
jal 0x802d77dc //print string
ori a2, s7, 0x7000
jal FLUSHSTRINGFUNC //flushstring
nop

jal XPOSRESETFUNC //update x pos of string
nop
jal POPMATRIXFUNC //pop matrix
nop
jal SCALEMATRIXFUNC //push scale matrix
nop
jal FIXSTRINGTRANSLATIONFUNC //fix translation after scale
nop

lw t1, 0x6df0 (S7) //current pos in string

lw t2, 0x6d14 (S7)
subu t2, t2, t1
sw t2, 0x6d14 (S7) //temp end pos
sw r0, 0x6df0 (S7)
sw r0, 0x6d54 (S7)
lw at, 0x6d1c (S7) //temp string
beq r0, r0, parse
sw at, 0x6d0c (S7) //*string return loc


//41 cmd
setsfx:
lw at, 0x6d0c (S7) //temp string
addiu at, at, 0x3
sw at, 0x6d0c (S7)

lw t9, 0x6df0 (S7) //current pos in string
lw t6, 0x6d14 (S7) //end pos in string
subu t7, t6, t9
bne t7, r0, parse
nop
jal U16LOADFUNC //load unaligned half, t8 in, t4 out
nop
beq r0, r0, parse
sh t4, 0x6d18 (S7)



//40 cmd
setspeed:
jal U16LOADFUNC //load unaligned half, t8 in, t4 out
nop
sh t4, 0x6d08 (S7)
lw t9, 0x6d0c (S7) //temp string
addiu t9, t9, 0x3
beq r0, r0, parse
sw t9, 0x6d0c (S7)


//FE cmd (not really a cmd but I made it one)
linebreak:
jal SETENVCOLORFUNC //set env color
sw t8, 0x18 (SP)
li t0, 0x9eff
lh at, 0x7000 (S7)
beq at, t0, noprintLB
lh a0, 0x6d10 (S7)
lh a1, 0x6d12 (S7)
jal 0x802d77dc //print string
ori a2, s7, 0x7000
lh a0, 0x6df4 (S7) //initial x cord
sh a0, 0x6d10 (S7) //temp x pos
lh a1, 0x6d12 (S7) //temp y pos
subiu a1, a1, 0xb
jal FLUSHSTRINGFUNC //flushstring
sh a1, 0x6d12 (S7)

//reset pos to zero so x pos doesn't get fuked, readjust string end pos to match
noprintLB:
lw at, 0x6df0 (S7) //current string pos
lw t1, 0x6d14 (S7) //temp string end pos
subu t1, t1, at
sw t1, 0x6d14 (S7)
sw r0, 0x6df0 (S7)
sw r0, 0x6d54 (S7)

lw t9, 0x6d0c (S7) //temp string pos
addu t9, t9, at
addiu t9, t9, 0x1

//reapply scaling and fix translation

jal POPMATRIXFUNC //pop matrix
sw t9, 0x6d0c (S7)
jal SCALEMATRIXFUNC //push scale matrix
nop
jal FIXSTRINGTRANSLATIONFUNC //fix translation after scale
nop

beq r0, r0, parse
nop



//70 cmd
AutoBoxEnd:
lw at, 0x6df0 (S7) //temp string pos
sw r0, 0x6df0 (S7)
lw t9, 0x6d0c (S7) //temp *string
addu t9, t9, at
addiu t9, t9, 0x1
sw t9, 0x6df8 (S7) //*string
li t7, 0xff
sb t7, 0x7000 (S7)
lh t1, 0x6d6c (S7) //original spd
bgezl t1, noresetVIs
sh t1, 0x6d08 (S7)
noresetVIs:
beq r0, r0, printnone
sw r0, 0x6dfc (S7) //end string pos


//71 cmd
ABoxEnd:
lui t9, 0x8034
lh t9, 0xafa2 (T9) //new buttons
andi t9, t9, 0x8000 //a press
andi t1, s6, 0x20
beq r0, t1, noarrowforend71
li t7, 0xff
li t7, 0x51
noarrowforend71:
li t6, 0xff
lw at, 0x6df0 (S7) //temp string pos
addu AT, S7, at
sb t6, 0x7001 (AT)
beq t9, r0, nonewchar
sb t7, 0x7000 (AT)
beq r0, r0, AutoBoxEnd
nop


//FF cmd (just stops display, does not remove string)
endofstring:

//draw previous string
jal SETENVCOLORFUNC //set env color
sw t8, 0x18 (SP)
lh a0, 0x6d10 (S7)
lh a1, 0x6d12 (S7)
jal 0x802d77dc //print string
ori a2, s7, 0x7000
jal FLUSHSTRINGFUNC //flushstring
nop
jal POPMATRIXFUNC //pop matrix
nop
jal SCALEMATRIXFUNC //push scale matrix
nop
jal FIXSTRINGTRANSLATIONFUNC //fix translation after scale
nop

//update *end of string
lw t1, 0x6df0 (S7)
lw t0, 0x6d14 (S7)
subu t0, t0, t1
sw t0, 0x6d14 (S7)

//update *temp string 
lw v1, 0x6d0c (S7) //*temp string
addu v1, v1, t1
addiu v1, v1, 0x1
sw v1, 0x6d0c (S7) //*temp string 


//set x pos to be init pos for line break
sw r0, 0x6d54 (S7)
jal XPOSRESETFUNC //update x pos of string
sw r0, 0x6df0 (S7) //temp x pos

//line break y pos
lh a1, 0x6d12 (S7) //temp y pos
subiu a1, a1, 0xb
sh a1, 0x6d12 (S7)


//6d58 will store returned dialog option
//6d59 will store currently displaying dialog option
//6d5A will store total number of options
//6d5B will store hovered dialog option

//end string after all options are drawn
lbu t0, 0x6d59 (S7)
lbu t1, 0x6d5A (S7)
BEQL t1, t0, printnone
sw v1, 0x6d5C (S7)
addiu t3, t0, 1
//draw arrow on current hovered line
sb t3, 0x6d59 (S7)
lb t2, 0x6d5b (S7)
bne t2, t3, noarrowenddialog
li t1, 0x53
li at, 0xff
sb at, 0x7001 (S7)


jal SETENVCOLORFUNC //set env color
sb t1, 0x7000 (S7)
lh a0, 0x6df4 (S7)
sh a0, 0x6d10 (S7)
lh a1, 0x6d12 (S7) //temp y pos
jal 0x802d77dc //print string
ori a2, s7, 0x7000
jal FLUSHSTRINGFUNC //flushstring
li t9, 0x1
jal XPOSRESETFUNC //update x pos of string
sw t9, 0x6df0 (S7)
jal POPMATRIXFUNC //pop matrix
nop
jal SCALEMATRIXFUNC //push scale matrix
nop
jal FIXSTRINGTRANSLATIONFUNC //fix translation after scale
nop

noarrowenddialog:
lw t1, 0x6d0c (S7)
sw r0, 0x6d54 (S7)
beq r0, r0, parse
sw r0, 0x6df0 (S7)




//the following is just a character not a cmd
normalchar:
lw t9, 0x6df0 (S7) //current pos in string
lw t8, 0x6d14 (S7) //end pos in string
subu t7, t8, t9
bne t7, r0, advancedchar

//here I check if I should advanced the end pos of the string, which should happen when a new chars time is here

lw at, 0x6d00 (S7) //VI of last character
lh t7, 0x6d08 (S7) //VIs per character
addu t8, t7, at //check if new char should be drawn
subu t8, s6, t8
//compares VI of last char drawn + wait time to current VI.
bltz t8, nonewchar

//here is a check to play a sfx
lh a0, 0x6d18 (S7) //sfx pointer
beq a0, r0, checkblip
sll a0, a0, 16
lui a1, 0x8033
ori a0, a0, 0x0081
jal 0x8031eb00
ori a1, a1, 0x31f0

checkblip:
lh a0, 0x6d1a (S7) //sfx check
beq a0, r0, drawnewchar
lui a1, 0x8033
lui a0, 0x16 //ice (placeholder)
ori a0, a0, 0xff81
jal 0x8031eb00
ori a1, a1, 0x31f0




drawnewchar:
lw t8, 0x6dfc (S7) //end pos in string
addiu t8, t8, 0x1
sw t8, 0x6dfc (S7)
lw t8, 0x6d14 (S7) //temp end pos in string
addiu t8, t8, 0x1
sw t8, 0x6d14 (S7)
sw s6, 0x6d00 (S7) //make current VI the VI of last char
advancedchar:
lw t9, 0x6df0 (S7) //current pos in string
addu at, s7, t9
lw t8, 0x6d0c (S7)
addu t8, t9, t8
lbu t7, 0x0 (T8) //char in string to write

//24 to 3D are lowercase letters
subiu t6, t7, 0x24
sltiu t6, t6, 0x1a
beq t6, r0, uppercaseincrement
lw t5, 0x6d54 (S7)
addiu t5, t5, 0x1
sw t5, 0x6d54 (S7)

uppercaseincrement:
sb t7, 0x7000 (AT)
li t6, 0xff
sb t6, 0x7001 (AT)
addiu t9, t9, 0x1
beq r0, r0, parse
sw t9, 0x6df0 (S7) //advanced position in string

//The string has gone as far as it needs to go
nonewchar:

//print whatever has not been printed already

jal SETENVCOLORFUNC //set env color
nop

lh a0, 0x6d10 (S7)
lh a1, 0x6d12 (S7)
jal 0x802d77dc
ori a2, s7, 0x7000

printnone:

lb at, 0x6d60 (S7)
beq at, r0, noscreenshake
nop
jal 0x802A50FC //shake screen
li a0, 0x1

noscreenshake:
//end a string
jal INSERTCHARENDDLFUNC //end string
sh r0, 0x6d62 (S7)
sw v0, 0x6d64 (S7) //end ptr
jal FLUSHSTRINGFUNC //flushstring
sw r0, 0x6df0 (S7)

end:
lw ra, 0x14 (SP)
jr ra
addiu sp, sp, 0x20


KEYBOARDSTARTLOC:
.incbin KEYBOARD_FILE

//.skip filesize("keyboard.bin")

.align

//STARTDIALOGWITHTHISOBJFUNC
//enable dialog with this object
//a0=x
//a1=y
//a2=*string
//everything but this object and mario will freeze, when dialog ends, everything will unfreeze (aka a 7C or a 7B cmd)

STARTDIALOGWITHTHISOBJFUNC:
addiu sp, sp, -0x18
sw ra, 0x14 (SP)
lw t0, 0x80361160
lw t1, 0x90 (T0)
bne t1, r0, checkstringendobj
lui s7, 0x807f
lw at, 0x6df8 (S7)
bne at, r0, objsetupdone
li t1, 1
jal SETUPSTRING //setup string
lh v1, 0x74 (T0)
ori v1, v1, 0x20
sh v1, 0x74 (T0)
lui at, 0x8034
lw t9, 0xd480 (AT)
ori t9, t9, 0x2
beq r0, r0, endobjsetup
sw t9, 0xd480 (AT)

objsetupdone:
beq r0, r0, endobjsetup
sw t1, 0x90 (T0)

checkstringendobj:
lw at, 0x6df8 (S7)
bne at, r0, endobjsetup
lh t6, 0x74 (T0)
xori t6, t6, 0x20
srl at, at, 8
sh t6, 0x74 (T0)
lui at, 0x8034
sw r0, 0xd480 (AT)
sw r0, 0x90 (T0)
lui t9, 0x8034
li t6, 0x0C400201 //idle
sw t6, 0xb17c (T9)

endobjsetup:
lw ra, 0x14 (SP)
jr ra
addiu sp, sp, 0x18


//SETUPSTRING
//sets up string
//a0=x
//a1=y
//a2=*string

SETUPSTRING:
addiu sp, sp, -0x18
sw ra, 0x14 (SP)
lui s7, 0x807f
sh a0, 0x6df4 (S7)
sh a1, 0x6df6 (S7)
sw a2, 0x6d68 (S7)
jal FLUSHSTRINGFUNC //flush string
sw a2, 0x6df8 (S7)
subiu t1, r0, 1
sh t1, 0x6d20 (S7)
sh t1, 0x6d22 (S7)
lw ra, 0x14 (SP)
jr ra
addiu sp, sp, 0x18



//load unaligned half, t8 holds address, returns t4
U16LOADFUNC:
Lbu t9, 0x1 (T8) //params of cmd
Lbu t4, 0x2 (T8) //params of cmd
sll t9, t9, 8
jr ra
or t4, t9, t4 //xi


//update x pos of string
XPOSRESETFUNC:
lw t1, 0x6df0 (S7) //current pos in string
lw t2, 0x6d54 (S7) //num lowercase letters
mtc1 t2, f14 //num lowercase
subu t1, t1, t2 //num uppercase letters
mtc1 t1, f12 //uppercase count
lui t0, 0x40a0//5
cvt.s.w f12, f12
mtc1 t0, f10 //5
cvt.s.w f14, f14
lui t3, 0x40d0//6.5
mtc1 t3, f2 //6.5
mul.s f2, f2, f12 //uppercase*6.5
mul.s f10, f10, f14//lowercase*5
lwc1 f4, 0x6d30 (S7) //x scale
add.s f2, f10, f2
mul.s f2, f2, f4 //letters*xscale
cvt.w.s f2, f2
mfc1 t2, f2
lh a0, 0x6d10 (S7)
addu a0, a0, t2
sw r0, 0x6d54 (S7)
jr ra
sh a0, 0x6d10 (S7)



//unaligned load, t8 in, a0 out

U32LOADFUNC:
andi t9, t8, 0x3 //unaligned address mod 4
addiu t9, t9, 0x1
sll t9, t9, 3
nor t7, r0, t9
addiu t7, t7, 0x21
subiu at, r0, 0x1
srlv t1, at, t7
sllv at, at, t9
LWR t7, 0x4 (T8)
and t7, t7, t1
LWL t6, 0x1 (T8)
and t6, t6, at
jr ra
or a0, t7, t6


//pop matrix
POPMATRIXFUNC:
lui at, 0x8034
lw v0, 0xb06c (AT)
lui t6, 0xBD00
sw t6, 0x0 (V0)
sw r0, 0x4 (V0)
addiu v0, v0, 0x8
jr ra
sw v0, 0xb06c (AT)

//scale matrix
SCALEMATRIXFUNC:
addiu sp, sp, -0x18
sw ra, 0x14 (SP)

lw a1, 0x6d30 (S7)
lw a2, 0x6d34 (S7)
lui a3, 0x3f80 //1 = scale
jal 0x802d7280 //scale matrix
ori a0, r0, 0x1 //push matrix

lw ra, 0x14 (SP)
jr ra
addiu sp, sp, 0x18


//fix string translate position
//fix positions for scale using this formula
//t=x/s-x=x(1/s-1)
FIXSTRINGTRANSLATIONFUNC:
addiu sp, sp, -0x18
sw ra, 0x14 (SP)
lwc1 f4, 0x6d30 (S7) //x scale
lwc1 f6, 0x6d34 (S7) //y scale
lui at, 0x3f80
mtc1 at, f20
lh a0, 0x6d10 (S7)
mtc1 a0, f0
lh t2, 0x6d12 (S7) //temp y
cvt.s.w f0, f0 //temp x
mtc1 t2, f2
div.s f4, f20, f4 //1/Sx
cvt.s.w f2, f2 //temp y
div.s f6, f20, f6 //1/Sy
sub.s f6, f6, f20 //1/Sy-1
sub.s f4, f4, f20 //1/Sx-1
mul.s f4, f4, f0 //X(1/Sx-1)
mul.s f6, f6, f2 //y(1/Sy-1)
mfc1 a1, f4 //x translate
mfc1 a2, f6 //y translate
or a3, r0, r0
jal 0x802d7070 //translate matrix
ori a0, r0, 0x2 //multiply
lw ra, 0x14 (SP)
jr ra
addiu sp, sp, 0x18



//insert char start ptr

INSERTCHARSTARTDLFUNC:
addiu sp, sp, -0x18
sw ra, 0x14 (SP)
jal 0x802d7384 //ortho matrix
nop
lui at, 0x8034
lw v0, 0xb06c (AT)
lui t6, 0x0600
sw t6, 0x0 (V0)
li t7, 0x02011cc8 //ia8 text string begin
sw t7, 0x4 (V0)
addiu v0, v0, 0x8
sw v0, 0xb06c (AT)
lw ra, 0x14 (SP)
jr ra
addiu sp, sp, 0x18



//insert char end ptr

INSERTCHARENDDLFUNC:
lui at, 0x8034
lw v0, 0xb06c (AT)
lui t6, 0x0600
sw t6, 0x0 (V0)
li t7, 0x02011d50 //ia8 text end
sw t7, 0x4 (V0)
addiu v0, v0, 0x8
jr ra
sw v0, 0xb06c (AT)


//flush string

FLUSHSTRINGFUNC:
lui at, 0x807f
flushnext:
lw t1, 0x7000 (AT)
beq t1, r0, endflush
sw r0, 0x7000 (AT)
beq r0, r0, flushnext
addiu at, at, 0x4
endflush:
li t1, 0xff
li t2, 0x9e
lui at, 0x807f
sb t2, 0x7000 (AT)
jr ra
sb t1, 0x7001 (AT)


//SET ENV COLOR
SETENVCOLORFUNC:
lw a0, 0x6d38 (S7)
lui at, 0x8034
lw v0, 0xb06c (AT)
lui t6, 0xFB00
sw t6, 0x0 (V0)
sw a0, 0x4 (V0)
addiu v0, v0, 0x8
jr ra
sw v0, 0xb06c (AT)


//completely reset string

COMPLETESTRINGRESETFUNC:
addiu sp, sp, -0x18
sw ra, 0x14 (SP)
lui at, 0x807f
lh a1, 0x6d20 (AT) //original music
lh t1, 0x6d22 (AT) //switched to music
beq a1, t1, endresetstring
nop
jal 0x80320544 //set music
or a0, r0, r0

endresetstring:
jal CLEANUPBUFFERFUNC //cleanup buffer
lui s7, 0x807f
jal FLUSHSTRINGFUNC //flush string
nop
jal INSERTCHARENDDLFUNC //end string
nop
addiu at, s7, 0x6d00
addiu t9, at, 0x100
clear1:
sw r0, 0x0 (AT)
bne t9, at, clear1
addiu at, at, 0x4

lw ra, 0x14 (SP)
jr ra
addiu sp, sp, 0x18



//signed unaligned half load

S16LOADFUNC:
lb t3, 0x1 (T8)
lbu t4, 0x2 (T8)
sll t3, t3, 8
jr ra
or a0, t3, t4 //SIGNED value


//ADDTOBUFFERFUNC
//a0 = location start
//a1 = num of bytes to add to buffer

ADDTOBUFFERFUNC:
li t7, 0x9d
lw at, 0x6d68 (S7) //original string start
subu t0, a0, at //offset
lw t1, 0x6ddc (S7) //buffer ptr
addu t1, t1, s7
sb t0, 0x6001 (T1)
srl t0, t0, 8
sb t0, 0x6000 (T1)
or t9, r0, r0
bufferaddloop:
lb t2, 0x0 (A0)
beq t9, a1, endbufferadd
addiu t1, t1, 1
sb t2, 0x6001 (T1)
sb t7, 0x0 (A0)
addiu a0, a0, 1
beq r0, r0, bufferaddloop
addiu t9, t9, 1

endbufferadd:
lw t1, 0x6ddc (S7) //buffer ptr
addiu a1, a1, 2
addu t1, a1, t1
jr ra
sw t1, 0x6ddc (S7)



//CLEANUPBUFFER
CLEANUPBUFFERFUNC:
li t7, 0x9d //padding byte
or t3, r0, s7
lw at, 0x6d68 (T3) //string start
checknextaddress:
lbu t1, 0x6000 (T3)
lbu t2, 0x6001 (T3)
sll t1, t1, 8
addu t1, t1, t2
beq t1, r0, bufferfullclean
addu t2, t1, at //ptr to load string from
sb r0, 0x6000 (T3)
sb r0, 0x6001 (T3)
addiu t3, t3, 2
removepaddingloop:
lbu t9, 0x0 (T2)
bne t9, t7, checknextaddress
lbu t8, 0x6000 (T3)
sb t8, 0x0 (T2)
sb r0, 0x6000 (T3)
addiu t3, t3, 1
beq r0, r0, removepaddingloop
addiu t2, t2, 1

bufferfullclean:
jr ra
sw r0, 0x6ddc (S7)
